package com.zzyl.serve.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.vo.NursingPlanVVO;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;

/**
 * 护理项目Service业务层处理
 *
 * @author zzyl
 * @date 2025-10-11
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    private static final String NURSING_PROJECT_ALL = "nursing_project_all";
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        boolean flag = this.save(nursingProject);
        //删除缓存
        deleteRedisCache();
        return  flag? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        boolean flag = this.updateById(nursingProject);
        //删除缓存
        deleteRedisCache();
        return  flag? 1 : 0;
    }


    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        boolean flag = this.removeById(id);
        //删除缓存
        deleteRedisCache();
        return flag? 1 : 0;
    }

    /**
     * 获取所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> getproject() {
        //先用redis查询
        List<NursingProjectVo> getproject = (List<NursingProjectVo>) redisTemplate.opsForValue().get(NURSING_PROJECT_ALL);
        //命中就返回，为命中就查询数据库
        if (CollectionUtils.isNotEmpty(getproject)) {
            return getproject;
        }
        //sql查询
        getproject = nursingProjectMapper.getproject();
        //存到redis
        redisTemplate.opsForValue().set(NURSING_PROJECT_ALL, getproject);
        return getproject;
    }

    /**
     * 删除缓存的方法，可以直接调用，不需要参数
     */
    private void deleteRedisCache() {
        // 删除缓存
        redisTemplate.delete(NURSING_PROJECT_ALL);
    }
}
